//
//  WBMainViewController.swift
//  学习微博
//
//  Created by zengyu on 2017/9/3.
//  Copyright © 2017年 weibo. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupChildControllers()
        setupComposeButton()
    }
    
     // MARK：按钮的监听方法
     // 撰写微博
     // FIXME：没有实现
    // private 能够保证方法私有，仅在当前对象被访问
    //@objc 允许这个函数在‘运行时’ 通过 OC 的消息机制被调用！
   @objc fileprivate func composeStatus() {
        print("撰写微博")
    }
   // lazy var composeButton: UIButton = UIButton.cz_textButton("", fontSize: 14, normalColor: UIColor.red, selectedColor: UIColor.blue)
    //MARK: 私有控件
    fileprivate lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
}

//extension 和OC的分类一样
//可以用相近的功能函数，放在一个extension 里面
//便于代码维护
//注意： 和OC的分类一样，extension 中不能定义属性
//MARK：- 设置界面
extension WBMainViewController {
    
    
   fileprivate func setupComposeButton() {
         tabBar.addSubview(composeButton)
        
        //计算按钮的宽度
        let count = CGFloat(childViewControllers.count)
        let w = tabBar.bounds.width / count - 1
        //设置的按钮的位置
        //CGRectInset 正数向内缩进，负数向外扩展
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)

    }
    
    
    //设置所有的自控制区
   fileprivate func setupChildControllers(){
        let array = [
            ["clsName":"WBHomeViewController", "title":"首页", "imageName":"home"],
            ["clsName":"WBMessageViewController", "title":"消息", "imageName":"message_center"],
            ["clsName":"UIViewControllr"],
            ["clsName":"WBDiscoverViewController", "title":"发现", "imageName":"discover"],
            ["clsName":"WBProfileViewController", "title":"我", "imageName":"profile"]
        ]
        
        var arrayM = [UIViewController]()
        
        for dict  in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
    }
    
    
    /// 使用字典创建一个自空气
    ///
    /// - Parameter dict: 信息字典[clsName, title, imageName]
    ///
    /// - Returns:  子控制器
    private func controller(dict: [String:String]) ->UIViewController {
        
        //1、取得字典内容
        guard let clsName = dict["clsName"],
            let tilte = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
            else {
                
                return UIViewController()
        }
        
        //2、创建视图控制器
        //1> 将 clsName 转换成 cls
        let vc = cls.init()
        vc.title = tilte
        //2> 设置图像
        //tabbar_home_selected
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysTemplate)
        
        //4、设置 tabbar 的标题字体颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted)
        //系统默认是12号字，修改字体大小，要设置 normal 的字体大小
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: UIControlState(rawValue: 0))
        let nav = WBNavigationController(rootViewController: vc)
        return nav
    }
    
}

